//
//  ViewController2.m
//  保存数据
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;

@property(nonatomic,strong)NSMutableArray *stuList;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(NSMutableArray *)stuList{
    if (_stuList==nil) {
        //如果之前已有存档文件，则从存档中读取出来以前的数据
        NSFileManager *fm=[NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]]) {
            _stuList=[NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else{
            _stuList=[NSMutableArray array];
        }
    }
    return _stuList;
}

//plist文件位置
-(NSString *)plistPath{
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"students.plist"];
    return filePath;
}

-(void)savePlist{
    [self.stuList writeToFile:[self plistPath] atomically:YES];
}
- (IBAction)add2:(id)sender {
    NSMutableDictionary *dict = [@{@"stuId":self.idTextField.text,
                                   @"stuName":self.nameTextField.text,
                                   @"stuAge":self.ageTextField.text} mutableCopy];
    [self.stuList addObject:dict];
    [self savePlist];
    // 刷新表格
    [self.tableView reloadData];
}
- (IBAction)modify2:(id)sender {
    for(NSMutableDictionary *dict in self.stuList)
    {
        if([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            dict[@"stuName"] = self.nameTextField.text;
            dict[@"stuAge"] = self.ageTextField.text;
            break;
        }
    }
    [self savePlist];
    // 刷新表格
    [self.tableView reloadData];
    
}
- (IBAction)remove2:(id)sender
{
    for(NSMutableDictionary *dict in self.stuList)
    {
        if([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            [self.stuList removeObject:dict];
            break;
        }
    }
    [self savePlist];
    // 刷新表格
    [self.tableView reloadData];

}


- (IBAction)find2:(id)sender {
    for(NSMutableDictionary *dict in self.stuList)
    {
        if([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            self.nameTextField.text = dict[@"stuName"];
            self.ageTextField.text = dict[@"stuAge"];
            break;
        }
    }

}
- (IBAction)findAll2:(id)sender {
    
    for(NSMutableDictionary *dict in self.stuList)
    {
        NSLog(@"%@ %@ %@",dict[@"stuId"],dict[@"stuName"],dict[@"stuAge"]);
    }
}

#pragma mark - tableViewDataSource Delegate Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stuList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    UILabel *label1 = [cell.contentView viewWithTag:101];
    UILabel *label2 = [cell.contentView viewWithTag:102];
    UILabel *label3 = [cell.contentView viewWithTag:103];
    
    NSDictionary *dict = self.stuList[indexPath.row];
    label1.text = dict[@"stuId"];
    label2.text = dict[@"stuName"];
    label3.text = dict[@"stuAge"];
    
    return cell;
}

@end
